import {
  getDateString
} from "@/assets/js/common.js";
let Controller = {
  mounted() {},
  activated() {
    this.requestTableList();
    this.getDowmList();
  },
  methods: {
    onTableCreate(id) {
      //跳转新建或编辑
      const name = 'createPurchaseOrder';
      this.$router.push({
        name: name,
        query: {
          data: JSON.stringify({id}),
        }
      })
    },
    onTableExport() {
      const arr = this.Model.selectionList;
      if (arr.length == 0) {
        this.$message.warning('请勾选需要导出的数据！')
        return;
      };
      const param = {
        name:'purchaseStorage',
        idList:arr
      };
      this.$ajax({
        method:'post',
        url:this.apiTest + `/supplier/common/exportExcel`,
        data:param,
        responseType:'arraybuffer'
      }).then(res=>{
        const data = res.data;
        let blob = new Blob([res.data],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
        let objectUrl = URL.createObjectURL(blob);
        window.open(objectUrl);
      })
      /* this.$ajax.post(this.apiTest + `/supplier/common/exportExcel`,param)
        .then(res => {
          const data = res.data;
          if(data.status == 200){
            this.downloadExcel();
          }
        }).catch(err => {
          this.$message.error('导出失败!')
        }) */
    },
    downloadExcel(){
      this.$ajax({
        metho
      })
    },
    onVoid(whWarehouseId) {
      this.$confirm('是否确认作废?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        const param = {
          whWarehouseId,
          status:60
        };
        if (param) {
          this.$ajax.post(this.apiTest + `/supplier/whWarehouse/cancelWarehouse`, param)
            .then(res => {
              const data = res.data;
              if (data.status == 200) {
                this.$message({
                  type:'success',
                  message:'作废操作成功！'
                });
                this.requestTableList();
              }else{
                this.$message.error(data.msg);
              }
            }).catch(err => {
              this.$message.error('未知错误，请联系管理员');
            })
        }
      }).catch(err=>{})
    },
    onWarehousing(row){
      const whWarehouseId = row.id;
      const inQuantity = row.inQuantity;
      this.$confirm(`本次入库数量( ${inQuantity} )件，是否确认入库?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        const param = {
          status:20,
          whWarehouseId
        };
        if (param) {
          this.$ajax.post(this.apiTest + `/supplier/whWarehouse/updateEntity`, param)
            .then(res => {
              const data = res.data;
              if (data.status == 200) {
                this.$message({
                  type:'success',
                  message:'入库操作成功！'
                });
                this.requestTableList();
              }else{
                this.$message.error(data.msg);
              }
            }).catch(err => {
              this.$message.error('未知错误，请联系管理员');
            })
        }
      }).catch(err=>{})
    },
    getDowmList() {
      this.$ajax.post(this.apiTest + `/supplier/whWarehouse/getDropBox/204`, {})
        .then(res => {
          const data = res.data;
          if (data.status == 200) {
            const result = data.result;
            this.Model.filterOptionData = result;
            let locationArr = [],
              wareHouseStatusArr = [],
              wareHouseTypeArr = [],
              supplierArr = [];
            for (let i in result.location) { //入库仓库
              locationArr.push({
                label: result.location[i],
                value: i
              });
            };
            for (let i in result.wareHouseStatus) { //入库单状态
              wareHouseStatusArr.push({
                label: result.wareHouseStatus[i],
                value: i
              });
            };
            for (let i in result.wareHouseType) { //入库单类型
              wareHouseTypeArr.push({
                label: result.wareHouseType[i],
                value: i
              });
            };
            for (let i in result.supplier) { //入库单类型
              supplierArr.push({
                label: result.supplier[i],
                value: i
              });
            };
            this.Model.filterOption.items[4].data = supplierArr; //供应商
            this.Model.filterOption.items[3].data = locationArr; //入库仓库
            this.Model.filterOption.items[2].data = wareHouseStatusArr; //入库单状态
            this.Model.filterOption.items[1].data = wareHouseTypeArr; //入库单类型
            this.Model.defaultFilterOption = _.cloneDeep(this.Model.filterOption)
          }
        })
    },
    requestTableList() {
      //请求主表数据
      const params = {
        condition: {
          ...this.getSearchInfoParam(),
          businessType: 30
        },
        ...this.Model.pager.params,
      };
      this.$ajax
        .post(this.apiTest + `/supplier/whWarehouse/list`, params)
        .then(response => {
          const data = response.data;
          this.Model.tableData = data.result.result;
          this.Model.pager.pageTotal = data.result.total;
          this.Model.pager.pageIndex = data.result.pageIndex;
        })
        .catch(error => {
          this.$message.error('请求错误');
        });
    },
    getSearchInfoParam() {
      //编辑高级搜索时间格式
      let data = _.cloneDeep(this.Model.searchFromData);
      if (data.onSaleApplyTime) {
        data.storageTimeStart = getDateString(data.onSaleApplyTime[0]);
        data.storageTimeEnd = getDateString(data.onSaleApplyTime[1]);
        data.onSaleApplyTime = undefined;
      }
      return data;
    },
    toTableDetail(row, flag) {
      //跳转入库详情
      const name = 'purchaseWarehousingDetail';
      this.$router.push({
        name: name,
        query: JSON.stringify({row,flag})
      })
    },
    onTableSelectionChange(val) {
      //表格多选操作
      if (val.length == 0) {
        this.Model.selectionList = [];
        return;
      }
      let arr = val.map((item, index, list) => {
        return item.id
      })
      this.Model.selectionList = arr;
    },
    onTableCurrentChange(val) {
      //表格分页-当前页变动操作
      this.Model.pager.params.pageIndex = val;
      this.requestTableList();
    },
    onTableSizeChange(val) {
      //表格分页-页数量变动操作
      this.Model.pager.params.pageSize = val;
      this.requestTableList();
    },
    onCardClick(color) {
      //页面card搜索点击操作
      this.Model.cardSelected = color;
    },
    getFilterData(val, item, option) {
      /* 
       saleOrderStatus订单状态[1]
       saleOrderTypes售中类型-状态联动[2][3]
       saleOrderChannels销售渠道[4][5]
       deliveryTypes发货方式[6]
       */
      // this.Model.filterOption.items = _.cloneDeep(option);
      for (let i = 0; i < 5; i++) {
        this.Model.filterOption.items[i] = option[i];
      }
      switch (item.key) {
        case 'saleOrderChannels':
          this.Model.filterOption.items[5].data = this.Model.saleManList[item.val].map(obj => {
            return {
              label: obj.value,
              value: obj.key
            }
          });
          this.Model.filterOption.items[5].value = undefined;
          break;
        case 'saleOrderTypes':
          this.Model.filterOption.items[3].data = this.Model.saleAfterOrderStatus[item.val].map(obj => {
            return {
              label: obj.value,
              value: obj.key
            }
          });
          this.Model.filterOption.items[3].value = undefined;
          break;
        default:
          break;
      };
      this.Model.searchFromData = val;
    },
    onFilterSearch(val) {
      this.requestTableList();
    },
    onFilterClear(val) {
      this.Model.searchFromData = _.cloneDeep(this.Model.defaultSearchFromData);
      this.Model.filterOption = _.cloneDeep(this.Model.defaultFilterOption);
    },
    onFilterExport(val) {},
    onTabChange(val) {
      switch (val) {
        case 1:
          break;
        case 2:
          break;
        case 3:
          break;
      }
    },
    setPage(pageIndex, pageSize, pageTotal, total) {
      this.Model.pager.params = {
        pageIndex,
        pageSize
      };
      this.Model.pager.total = total;
    },
    getStatusText(type, value) {
      let text = '';
      switch (type) {
        case 'payStatus':
          /* 0未支付/1部分支付/2已支付 */
          switch (value) {
            case 0:
              text = '未支付';
              break;
            case 1:
              text = '部分支付';
              break;
            case 2:
              text = '已支付';
              break;
            default:
              text = '暂无';
              break;
          }
          break;
        case 'orderStatus':
          /* 0待付款/1待审核/2待发货/3待提货/4部分已发货/5待收货/6已完成/7已关闭 */
          switch (value) {
            case 0:
              text = '待付款';
              break;
            case 1:
              text = '待审核';
              break;
            case 2:
              text = '待发货';
              break;
            case 3:
              text = '待提货';
              break;
            case 4:
              text = '部分已发货';
              break;
            case 5:
              text = '待收货';
              break;
            case 6:
              text = '已完成';
              break;
            case 7:
              text = '已关闭';
              break;
            default:
              text = '暂无';
              break;
          }
          break;
        case 'aftersaleStatus':
          /* 
            售后：0退款中/1退款成功/2退款拒绝/ 
            售中：3待审核/4已取消/5变更成功/6待用户支付/7待平台退款
           */
          switch (value) {
            case 0:
              text = '退款中';
              break;
            case 1:
              text = '退款成功';
              break;
            case 2:
              text = '退款拒绝';
              break;
            case 3:
              text = '待审核';
              break;
            case 4:
              text = '已取消';
              break;
            case 5:
              text = '变更成功';
              break;
            case 6:
              text = '待用户支付';
              break;
            case 7:
              text = '待平台退款';
              break;
            default:
              text = '暂无';
              break;
          }
          break;
        case 'orderType':
          /* 10现货订单/20定制订单/30售中订单/40售后订单 */
          switch (value) {
            case 10:
              text = '现货订单';
              break;
            case 20:
              text = '定制订单';
              break;
            case 30:
              text = '售中订单';
              break;
            case 40:
              text = '售后订单';
              break;
            default:
              text = '暂无';
              break;
          }
          break;
        case 'afterSaleType':
          /* （10售中变更 20退款 30升级换款 40常规售后） */
          switch (value) {
            case 10:
              text = '售中变更';
              break;
            case 20:
              text = '退款';
              break;
            case 30:
              text = '升级换款';
              break;
            case 40:
              text = '常规售后';
              break;
            default:
              text = '暂无';
              break;
          }
          break;
        default:
          break;
      }
      return text;
    },
    cancelInSale(id) {
      this.$confirm('是否确认取消售中?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        this.$ajax
          .get(this.apiTest + `/sale/onSale/order/sale/cancel/${id}`)
          .then(response => {
            const data = response.data;
            if (data.status == 200 && data.result == 1) {
              this.$message({
                type: 'success',
                message: '已成功'
              });
              this.requestTableList();
            };
          })
          .catch(error => {
            this.$message.error('请求错误');
          });
      }).catch(() => {

      });

    },
    cancelRefund(id) {
      this.$confirm('是否确认取消退款?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        this.$ajax
          .get(this.apiTest + `/sale/onSale/order/refund/cancel/${id}`)
          .then(response => {
            const data = response.data;
            if (data.status == 200 && data.result == 1) {
              this.$message({
                type: 'success',
                message: '已成功'
              });
              this.requestTableList();
            };
          })
          .catch(error => {
            this.$message.error('请求错误');
          });
      }).catch(() => {

      });

    },
    sendlRefund(id) {
      this.$ajax
        .get(this.apiTest + `/sale/onSale/order/refund/Initiate/${id}`)
        .then(response => {
          const data = response.data;
          if (data.status == 200 && data.result == 1) {
            this.$message({
              type: 'success',
              message: '已成功'
            });
          }
        })
        .catch(error => {
          this.$message.error('请求错误');
        });
    },
    showPayDialog(id) {
      this.Model.dialogFormVisible = true;
      this.Model.nowSelectId = id;
    },
    pay() {
      const form = this.Model.dialogForm;
      const params = {
        payMethod: form.payMethod,
        paymentMoney: form.paymentMoney,
        serialNumber: form.serialNumber,
        payTime: getDateString(form.payTime),
        orderId: this.Model.nowSelectId
      };
      this.$ajax
        .post(this.apiTest + `/sale/onSale/order/afterSale/pay`, params)
        .then(response => {
          const data = response.data;
          if (data.status == 200) {
            this.$message({
              type: 'success',
              message: '已成功'
            });
            this.requestTableList();
          };
          this.Model.dialogFormVisible = false;
        })
        .catch(error => {
          this.$message.error('请求错误');
          this.Model.dialogFormVisible = false;
        });
    },
  }
}
export default Controller;
